﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Linq;

namespace VisualRibbonEditor
{
	/// <summary>
	/// Methods for hiding and showing buttons using HideCustomActions
	/// </summary>
	public static class Hider
	{
		public static void HideControl( string controlId )
		{
			XElement customActions = MainWindow.CurrentSolution.RibbonDiffXml.Descendants("CustomActions").FirstOrDefault();
			if( customActions != null)
			{
				string prefix = MainWindow.CurrentSolution.GetIdPrefix() ?? string.Empty;
				string hideActionId = prefix + controlId + ".HideAction";
				
				XElement hideCustomAction = new XElement("HideCustomAction");
				hideCustomAction.SetAttributeValue("Location", controlId);
				hideCustomAction.SetAttributeValue("HideActionId", hideActionId);

				customActions.Add(hideCustomAction);
			}
		}

		public static void ShowControl(string controlId)
		{
			XElement customActions = MainWindow.CurrentSolution.RibbonDiffXml.Descendants("CustomActions").FirstOrDefault();
			XElement hideCustomAction =
				customActions.Descendants("HideCustomAction").Where(x => x.AttributeValue("Location") == controlId).FirstOrDefault();

			if( hideCustomAction != null)
			{
				hideCustomAction.Remove();
			}
		}

		public static bool IsControlHidden(string controlId)
		{
			XElement customActions = MainWindow.CurrentSolution.RibbonDiffXml.Descendants("CustomActions").FirstOrDefault();
			XElement hideCustomAction =
				customActions.Descendants("HideCustomAction").Where(x => x.AttributeValue("Location") == controlId).FirstOrDefault();

			return (hideCustomAction != null);
		}

	}
}
